package me.chan.sort;

import static me.chan.util.SortUtil.*;
/**
 * Created by JanGin on 2017/9/11.
 */
public class BubbleSort {

    //缺点: 时间复杂度 O(n^2)
    public static void sort(Comparable[] a) {
        //
        int exch = 0;
        for (int i = 0; i < a.length - 1; i++) {
            for (int j = 0; j < a.length - 1 - i ; j++) {
                if (less(a[j+1], a[j])) {
                    exch(a, j, j+1);
                    ++exch;
                }
            }
            //System.out.println("第"+(i+1)+"轮排序结果:");
            //show(a);
            System.out.println("第"+(i+1)+"趟排序的交换元素次数是:"+exch);
            exch = 0;
        }
    }

    public static void main(String[] args) {
        //Integer[] arr = {20, 17, 12, 9, 10, 23, 13, 24, 19, 93, 8, 15, 98, 4, 68 };
        //Comparable[] arr = {'E', 'E', 'G', 'M', 'R', 'A', 'C', 'E', 'R', 'T'};
        Integer[] arr = {50, 40, 95, 20, 15, 70, 60, 45, 80};
        show(arr);

        sort(arr);

        //[ 4, 8, 9, 10, 12, 13, 15, 17, 19, 20, 23, 24, 68, 93, 98 ]
        //[ A, C, E, E, E, G, M, R, R, T ]
        show(arr);
    }
}
